using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Beta_fundies_controls_quotes_votepage : Controls.DataManagerQuoteControl
{
    private string CurrentIP
    {
        get
        {
            return HttpContext.Current.Request.UserHostAddress;
        }
    }


    protected override void ItemChanged()
    {
        if (CurrentQuote != null && CurrentQuote.ApprovalStatus == DAL.Quote.QuoteStatus.Approved)
        {
            mviewVote.SetActiveView(viewVote);

            AddVote();

            ShowQuote();
        }
        else
        {
            mviewVote.SetActiveView(viewError);
        }
    }

    private void AddVote()
    {
        if (!DAL.Votes.HasUserVoted(CurrentQuote, CurrentIP))
        {
            int vote;
            if (Helpers.QueryStringHelper.TryGetInt32("vote", out vote))
            {
                if (vote >= 1 && vote <= 5)
                {
                    lblMsg.Visible = true;
                    DAL.Vote myVote = new DAL.Vote();
                    myVote.Quote = CurrentQuote;
                    myVote.IPAddress = CurrentIP;
                    DAL.Votes.Add(myVote);

                    CurrentQuote.TotalVotes += vote;
                    CurrentQuote.NumberOfVoters += 1;
                    DAL.Quotes.Save(CurrentQuote);
                }
            }
        }
    }

    private void ShowQuote()
    {
        List<DAL.Quote> myList = new List<DAL.Quote>(1);
        myList.Add(CurrentQuote);
        QuoteList1.SetDataSource(myList);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CurrentQuote = GetItemFromQueryString();
        }
    }
}
